﻿using Autofac;
using System.Reflection;
using Module = Autofac.Module;

namespace Project.Core.Config.Config.Autofac {
    public class AutofacModule : Module {

        private readonly string?[] _assemblyNames;
        public AutofacModule(string?[] assemblyNames) {
            _assemblyNames = assemblyNames;
        }

        protected override void Load(ContainerBuilder builder) {
            var assemblies = _assemblyNames.Select(x => Assembly.Load(x)).ToList();
            //assemblies.Add(Assembly.GetExecutingAssembly());

            //通过命名注入
            builder.RegisterAssemblyTypes(assemblies.ToArray())
                            .Where(t => t.FullName.EndsWith("Impl") && !t.IsAbstract) //类名以Impl结尾，且类型不能是抽象的　
                            .InstancePerLifetimeScope() //作用域生命周期
                            .AsImplementedInterfaces()  //是以接口方式进行注入
                            .PropertiesAutowired(); //属性注入
        }


    }
}
